<?php
class Rubrique {

	public $id;
	public $libelle;
		
	
	
	public function __construct($id='') {
		if($id != ''){
			$this->id = $id;
			$this->load();
		}
	}

	public function load() {
		if(isset($this->id)){
			$db = Database::getInstance();
			$sql= 'SELECT idRubrique, 
                                      libelle,
                                      date
                               FROM rubrique
			       WHERE idrubrique = "'.$this->id.'"';					
						  
						  
			if ($result = $db->fetch($sql)){				
				$this->libelle = $result[0]['libelle'];					
				
				return true;
			}
			return false;	
		}
	}
	
	public function __toString(){
		return $this->libelle.' ';
	
	}
	
	
	public static function getAll() {
		$i=0;
		$array = null;
		
		$db = Database::getInstance();
			$sql= 'SELECT idRubrique,
                                      libelle,
                                      date						  
				FROM rubrique';					
						  
						  
			if ($result = $db->fetch($sql)){	
				foreach($result as $row){ 
					/*$tmp_rubrique = new rubrique();		
                                        $tmp_rubrique->id = $row['idRubrique'];
					$tmp_rubrique->libelle = $row['libelle'];
					$tmp_rubrique->date = $row['date'];
					
					$array[] =  $tmp_rubrique;*/
                                         $array[$i]['id'] = $row['idRubrique'];
                                         $array[$i]['libelle'] = $row['libelle'];
                                         $array[$i]['date'] = $row['date'];
                                         
                                         $i++;
				}		
			}
			return $array;			
	}
	
		
	public function addRubrique($libelle){
	/* On �chappe les information ins�r�es par l'utilisateur*/
		
		$db = Database::getInstance();
		$sql= 'INSERT INTO `rubrique` (
		`idRubrique` ,
		`libelle` ,
                `date`
		)
		VALUES 
		(
		NULL ,
		"'.$libelle.'",		
		CURRENT_TIMESTAMP
		)';
		
		if($db->exec($sql)){
			return true;
		}
		
		return false;
		
	}
	
	public static function deleteRubrique($idRubric){            
           // DELETE FROM `android`.`rubrique` WHERE `rubrique`.`idRubrique` = 9
            
            $db = Database::getInstance();
		$sql= 'DELETE FROM `rubrique` 
                        WHERE `idRubrique` = '.$idRubric.'';
		
		if($db->exec($sql)){
			return true;
		}
		
		return false;
        }
}

?>